package com.woniu.service.impl;

import com.woniu.dao.StudentDao;
import com.woniu.dao.impl.StudentDaoImpl;
import com.woniu.entity.Student;
import com.woniu.exception.PhoneExsitsException;
import com.woniu.exception.NameExsitsException;
import com.woniu.service.StudentService;

import java.io.*;
import java.util.List;
import java.util.Scanner;

public class StudentServiceImpl implements StudentService {
    StudentDaoImpl studentDao = new StudentDaoImpl();


    @Override
    public void StudentRegister(Student student){
        if(studentDao.selectStudentName(student.getName()) != null){
            throw new NameExsitsException("名字存在");
        }

        if(studentDao.selectStudentPhone(student.getPhone()) != null){
            throw new PhoneExsitsException("电话存在");
        }

        studentDao.insertStudent(student);
    }

    @Override
    public void StudentLogin(String name, String phone) {
        if(studentDao.selectStudentName(name) == null){
            throw new NameExsitsException("名字错误");
        }
        if(studentDao.selectStudentPhone(phone) == null){
            throw new PhoneExsitsException("电话错误");
        }
    }

    @Override
    public void GetStudentAll() {
        StudentDao studentDao = new StudentDaoImpl();
        List<Student> students = studentDao.selectStudentAll();
        for(Student student : students){
            System.out.println(student.getId()+"-"+student.getName()+"-"+student.getAge()+"-"+student.getGender()+"-"+student.getPhone());
        }
    }

    @Override
    public void DeleteStudent(int sub) {
        StudentDao studentDao = new StudentDaoImpl();
        List<Student> students = studentDao.selectStudentAll();
        students.remove(sub);
        File file = studentDao.ClearStudent();
        try(BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
            for(Student student : students){
                br.write(student.getId()+"-"+student.getName()+"-"+student.getAge()+"-"+student.getGender()+"-"+student.getPhone());
                br.newLine();
                br.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void UpdateStudent(int sub) {
        StudentDao studentDao = new StudentDaoImpl();
        List<Student> students = studentDao.selectStudentAll();
        File file = studentDao.ClearStudent();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入修改后的id");
        String id = sc.next();
        students.get(sub).setId(id);
        System.out.println("请输入修改后的name");
        String name = sc.next();
        students.get(sub).setName(name);
        System.out.println("请输入修改后的age");
        String age = sc.next();
        students.get(sub).setAge(age);
        System.out.println("请输入修改后的gender");
        String gender = sc.next();
        students.get(sub).setGender(gender);
        System.out.println("请输入修改后的phone");
        String phone = sc.next();
        students.get(sub).setPhone(phone);
        try(BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
            for(Student student : students){
                br.write(student.getId()+"-"+student.getName()+"-"+student.getAge()+"-"+student.getGender()+"-"+student.getPhone());
                br.newLine();
                br.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
